//
//  GQHHomeController.m
//  Xanthium
//
//  Created by GuanQinghao on 08/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHHomeView.h"

#pragma mark Controller
#import "GQHHomeController.h"
#import "GQMyEmptyCarController.h"

#import "GQHMessagesController.h"
#import "GQHCustomerServicesController.h"

#import "GQHFreeController.h"
#import "GQHSupplyController.h"

#import "GQHOrderController.h"
#import "GQHRouteFreightsController.h"
#import "GQHQutationsController.h"
#import "GQHSuppliesController.h"

#import "GQHDriverProfileController.h"
#import "GQHDriverAuthorizationController.h"


#pragma mark CocoaPods

#pragma mark ----------Header End----------


@interface GQHHomeController () <GQHHomeViewDelegate> {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHHomeView *rootView;
}

@end


@implementation GQHHomeController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHHomeView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_navigationBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self getMessageCount];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark ---- 获取消息数量
- (void)getMessageCount
{
    NSString * url = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"buMessageReceiveUser/1/unReadNum"];
    [GQHNetHelper qh_requestPOST:url parameters:@{@"userId":USER_GET_ID} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        rootView.badgeCount = [NSString stringWithFormat:@"%@", data];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark --Delegate
- (void)qh_provideSupplyOrFree:(UIButton *)sender {
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        DLog(@"发布空车");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 发布空车
            GQHFreeController *freeController = [[GQHFreeController alloc] init];
            freeController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:freeController animated:YES];
        } unallowed:nil];
    } else {
        DLog(@"发布货源");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            // 发布货源
            GQHSupplyController *supplyController = [[GQHSupplyController alloc] init];
            supplyController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:supplyController animated:YES];
        } unallowed:nil];
    }
}

- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.row) {
        DLog(@"运单管理");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 运单管理
            GQHOrderController *orderController = [[GQHOrderController alloc] init];
            orderController.qh_orderState = @"待装货";
            orderController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:orderController animated:YES];
        } unallowed:nil];
    }
    
    if (1 == indexPath.row) {
        DLog(@"回程货源");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 回程货源
            GQHRouteFreightsController *routeFreightsController = [[GQHRouteFreightsController alloc] init];
            routeFreightsController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:routeFreightsController animated:YES];
        } unallowed:nil];
    }
    
    if (2 == indexPath.row) {
        DLog(@"我的报价");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 我的报价
            GQHQutationsController *qutationsController = [[GQHQutationsController alloc] init];
            qutationsController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:qutationsController animated:YES];
        } unallowed:nil];
    }
    
    if (3 == indexPath.row) {
        DLog(@"在途追踪");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 在途追踪
            GQHOrderController *orderController = [[GQHOrderController alloc] init];
            orderController.qh_orderState = @"运输中";
            orderController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:orderController animated:YES];
        } unallowed:nil];
    }
    
    if (4 == indexPath.row) {
        DLog(@"我的货源");
        
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 我的货源
            GQHSuppliesController *suppliesController = [[GQHSuppliesController alloc] init];
            suppliesController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:suppliesController animated:YES];
        } unallowed:nil];
    }
    
    if (indexPath.row == 5)
    {
        DLog(@"我的发布");
        [self qh_checkAccessPermissionType:GQHAccessPermissionTypeJunior allowed:^{
            
            /// 我的报价
            GQMyEmptyCarController *empty = [[GQMyEmptyCarController alloc] init];
            empty.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:empty animated:YES];
        } unallowed:nil];
    }
}

- (void)qh_checkNotification:(UIButton *)sender {
    DLog(@"查看消息");
    
    GQHMessagesController *messagesController = [[GQHMessagesController alloc] init];
    messagesController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:messagesController animated:YES];
}

- (void)qh_contactCustomerService:(UIButton *)sender {
    DLog(@"查看客服");
    
    GQHCustomerServicesController *customerServicesController = [[GQHCustomerServicesController alloc] init];
    customerServicesController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:customerServicesController animated:YES];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
